package borge.web.teamspirit.client.page.team.widgets;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Team;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.CreateDataWidget;
import borge.web.teamspirit.client.page.shared.widgets.InputWrapper;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;

public class CreatePlayerWidget extends CreateDataWidget implements ClickHandler {
	
	private TeamPlayer iPlayer;
    
	public CreatePlayerWidget(WidgetEventHandler aEventHandler){
		super(aEventHandler);
	
		
		iPanel.add(iTable);
		initWidget(iPanel);
	}
	

	private void createDataTable(){

		TeamSpiritConstants lsc = TeamSpirit.getConstants();
		int row = 0;
		final int maxLength = 30;
		
		if(iPlayer == null)
			iPlayer = new TeamPlayer();
		
		iTable.setWidget(row++, 0, new Label("Name: " + iPlayer.getPersonName()));
		addTableRow(row++, new InputWrapper(lsc.active(), createCheckBox(iPlayer.isActive()), true));		
		addTableRow(row++, new InputWrapper(lsc.position(), createTextBox(iPlayer.getPosition(), maxLength), false));
		addTableRow(row++, new InputWrapper(lsc.shirt_number(),createTextBox(iPlayer.getShirtNumber(), maxLength), false));
		addTableRow(row++, new InputWrapper(lsc.join_date(), createDateBox(iPlayer.getJoinDate()), false));
		addTableRow(row++, new InputWrapper(lsc.other_info(), createTextArea(iPlayer.getOtherInfo(), maxLength, 5), false));
		
		
		addButton(row, 0, this);
	}
        
    @SuppressWarnings("unchecked")
    public TeamPlayer getDataObject(){
        return iPlayer;
    }
	
	public void onClick(ClickEvent aEvent){
		
	    // Verify input data
	    // * make sure
	    
		postEvent(WidgetEventType.PLAYER_CREATED);
		
	}


	@Override
	public void updateData() {
		
		iPlayer = (TeamPlayer)SessionData.sEditObject;
		SessionData.sEditObject = null;
		createDataTable();
	}


}
